/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.neuralnetwork;

import java.util.ArrayList;
import java.util.Random;

/**
 *
 * @author afspear
 */
public class Network {

    public ArrayList<Layer> matrix;
    public ArrayList<Double> data;
    public Layer outputLayer;

    public void fillInputsWithdata(ArrayList<Double> data) {
        Random random = new Random();

        for (Perceptron percepton : matrix.get(0)) {
            for (Double dataDouble : data) {
                Input input = new Input(percepton, random.nextDouble(), dataDouble);
                percepton.addInputs(input);
            }
        }
    }
//
//
//    }
//    public void newData(ArrayList<String> data) {
//        foreach(Perceptron 
//        neuron in matrix[0]
//        
//            )
//            {
//                for (int i = 0; i < data.Count; i++) {
//                neuron.inputs[i].data = Double.Parse(data[i]);
//            }
//        }
//    }

    private void activatePerceptron(Perceptron perceptron, Layer layer, ArrayList<Double> outputs) {
        //perceptron.getInput ();
        perceptron.computeOutput();
        if (layer.name.equals(LayerName.Output)) {
            outputs.add(perceptron.output);
        }
    }

    public ArrayList<Double> pushUpAnswer() {
        ArrayList<Double> outputs = new ArrayList<Double>();
        for (Layer layer : matrix) {
            for (Perceptron percetpron : layer) {
                activatePerceptron(percetpron, layer, outputs);
            }
        }
        return outputs;
    }

    public Network(int numberOfInputs, int numberOfOutputs, int numberOfHiddenLayers) {
        matrix = new ArrayList<Layer>();

        int i;

        //add the hidden layers
        for (i = 0; i < numberOfHiddenLayers; i++) {
            matrix.add(new Layer(LayerName.Hidden, i));
        }
        //add the output layer
        matrix.add(new Layer(LayerName.Output, ++i));

        outputLayer = matrix.get(matrix.size() - 1);

        int mean = (numberOfInputs + numberOfOutputs) / 2;
        //must have at least 2 perceptrons in the hidden layer
        if (mean == 1) {
            mean = 2;
        }

        int hiddenLayerCounter = 0;

        for (Layer layer : matrix) {
            if (layer.name == LayerName.Hidden) {
                for (i = 0; i < mean; i++) {
                    layer.add(new Perceptron(layer, i));
                }
                hiddenLayerCounter++;
            } else {
                for (i = 0; i < numberOfOutputs; i++) {
                    layer.add(new Perceptron(layer, i));
                }
            }
        }

        Random random = new Random();

        for (Layer layer : matrix) {
            if (layer.level > 0) 
            {
                int index = matrix.indexOf(layer);
                for (Perceptron p : layer) 
                {
                    for (Perceptron q : matrix.get(index - 1)) 
                    {
                        double number;
                        //if (random.nextDouble() > .5) 
//                        {
                            number = random.nextDouble();
//                        } 
//                        else 
//                        {
//                            number = -random.nextDouble();
//                        }

                        Input input = new Input(q, p, number, Double.NaN);

                        p.addInputs(input);
                        q.addOutputs(input);
                    }
                }

            }

        }
    }
}
